<?php
	
	function dataStore_FindUser($xml, $username, $password)
	{
		$res = null;
		
		foreach ($xml->user as $user)
		{
			if (MD5($user['name']) == $username && MD5($user['password']) == $password)
			{
				$res = $user;
				break;
			}
		}
		
		return $res;
	}
	
	function dataStore_loadContacts($storageFile, $username, $password, $group, $contact)
	{
		$contacts = null;
    
		$xml = simplexml_load_file ($storageFile);
		
		$curUser = dataStore_FindUser($xml, $username, $password);
		
		
		if ($curUser != null)
		{
			foreach ($curUser->group as $group)
			{
				foreach ($group->contact as $contact)
				{
        	$contactres = getNewContact();
        	
        	foreach($contact->children() as $tagname => $tag)
        	{
        		$contactres[$tagname] = (string)$tag;
        	}
        	$contactres['id'] = (string)$contact['id'];
        	$contactres['groupName'] = (string)$group['name'];
        	
					$contacts[] = $contactres;
				}
			}
		}
		else
		{
			echo 'wrong Username or Password';
		}
		
		return $contacts;
	}
	
	function dataStore_saveXML($storeFile, $username, $password, $contacts)
	{
		$xml = simplexml_load_file ($storeFile);
		
		$curUser = dataStore_FindUser($xml, $username, $password);
		
		if ($curUser != null)
		{
			foreach ($contacts as $contact)
			{
				dataStore_addContactToXML($contact, $curUser);
			}
		}
		
		$handle = fopen($storeFile, 'w+');
		if (!$handle)
		{
			exit;
		}
		
		$res = fwrite($handle, $xml->asXML());
		
		if (!$res)
		{
			exit;
		}
		
		fclose($handle);
	}
	
	function dataStore_addContactToXML($contactToAdd, $user)
	{
		$curGroup = null;
		foreach ($user->group as $group)
		{
			if ((String)($group['name']) != $contact.groupName)
			{
				continue;
			}
			$curGroup = $group;
		}
		
		$alreadyExists = false;
		if ($curGroup == null)
		{
			$curGroup = $user->addChild('group', '');
			$curGroup['name'] = $contactToAdd['groupName'];
		}
		else
		{
			foreach ($group->contact as $contact)
			{
				if ((String)($contact['id']) == $contactToAdd.id)
				{
					$alreadyExists = true;
					break;
				}
			}
		}
		
		if (!$alreadyExists)
		{
			$newContact = $curGroup->addChild('contact', '');
			$newContact->addAttribute('id', $contactToAdd['id']);
			
			foreach ($contactToAdd as $key => $value)
			{
				if ($value != "" && $key != 'groupName' && $key != 'id')
				{
					$newContact->addChild($key, $value);
				}
			}
		}
	}
	
	function dataStore_userExists($storeFile, $username, $password)
	{
		$xml = simplexml_load_file ($storeFile);
		$result = '<result>false</result>';
		
		$curUser = dataStore_FindUser($xml, $username, $password);
		
		if ($curUser != null)
		{
			$result = '<result>true</result>';
		}
		
		return $result;
	}
	
	function dataStore_AddContact($storeFile, $username, $password, $contactToAdd)
	{
		$xml = simplexml_load_file ($storeFile);
		
		$curUser = dataStore_FindUser($xml, $username, $password);
		
		
		if ($curUser != null && $contactToAdd['groupName'] != '')
		{
			$groupAlreadyExists = false;
			foreach ($curUser->group as $group)
			{
				if ($contactToAdd['groupName'] == $group['name'])
				{
					$groupAlreadyExists = true;
					$curGroup = $group;
					break;
				}
			}
			
			if (!$groupAlreadyExists)
			{
				$curGroup = $curUser.addChild('group', '');
				$curGroup['name'] = $contactToAdd['groupName'];
			}
			
			$newId = MD5(implode($contactToAdd));
			
			foreach ($curGroup->contact as $existingContact)
			{
				if($existingContact['id'] == $newId)
				{
					exit;
				}
			}
			
			
			$newContact = $curGroup->addChild('contact', '');
			$newContact['id'] = $newId;
			
			foreach ($contactToAdd as $key => $value)
			{
				if ($value != "" && $key != 'groupName')
				{
					$newContact->addChild($key, $value);
				}
			}
			
			$handle = fopen($storeFile, 'w+');
			if (!$handle)
			{
				exit;
			}

			$res = fwrite($handle, $xml->asXML());

			if (!$res)
			{
				exit;
			}

			fclose($handle);
			
			return 'Contact '.$newId.' Added';
		}
	}
	
			
	function dataStore_DeleteContact($storeFile, $username, $password, $contactId)
	{
		$xml = simplexml_load_file ($storeFile);

		$curUser = dataStore_FindUser($xml, $username, $password);
		
		
		if ($curUser != null && $contactId != '' && $contactId != null)
		{
			$done = false;
			foreach ($curUser->group as $group)
			{
				$childs = $group->children();
				for ($i=0; $i<count($childs); $i++)
				{
					
					$contact = $childs[$i];
					if($contact['id'] == $contactId)
					{
						unset($group->contact[$i]);
						$done = true;
						break;
					}
				}
			}

			if ($done)
			{
				$handle = fopen($storeFile, 'w+');
				if (!$handle)
				{
					exit;
				}

				$res = fwrite($handle, $xml->asXML());

				if (!$res)
				{
					exit;
				}

				fclose($handle);
			}
			
			$res = $contactId;
		}
		
		return '<result>'.$res.'</result>';
	}
?>